package com.palm.hubo2o.ui;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.palm.hubo2o.BaseActivity;
import com.palm.hubo2o.R;
import com.palm.hubo2o.model.PalmSMS;
import com.palm.hubo2o.model.Registration;
import com.palm.hubo2o.model.User;
import com.palm.hubo2o.network.PalmCallBack;
import com.palm.hubo2o.network.PalmRetrofit;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.network.Result;
import com.palm.hubo2o.util.CountDownUtils;
import com.palm.hubo2o.util.PromptUtil;
import com.palm.hubo2o.view.ProgressDialogCompat;
import com.palm.palmutils.MD5;
import com.palm.palmutils.StringUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by wongqi on 16/6/20.
 */
public class RegisterActivity extends BaseActivity {
    @Bind(R.id.phone)
    EditText phone;
    @Bind(R.id.codes)
    EditText codes;
    @Bind(R.id.get_codes)
    Button getCodes;
    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.password_confirm)
    EditText passwordConfirm;
    @Bind(R.id.station)
    EditText station;
    @Bind(R.id.station_by_location)
    ImageView stationByLocation;
    @Bind(R.id.station_by_scan)
    ImageView stationByScan;
    @Bind(R.id.submit_button)
    Button submitButton;

    private CountDownTimer countDown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDown != null)
            countDown.cancel();
    }

    @OnClick({R.id.get_codes, R.id.station_by_location, R.id.station_by_scan, R.id.submit_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.get_codes:
                getCodes();
                break;
            case R.id.station_by_location:
                break;
            case R.id.station_by_scan:
                break;
            case R.id.submit_button:
                if (phone == null || password == null || passwordConfirm == null || station == null || codes == null) {
                    return;
                }
                String mobile = phone.getText().toString();
                String passwordStr = password.getText().toString();
                String passwordConfirmStr = passwordConfirm.getText().toString();
                String stationStr = station.getText().toString();
                String codesStr = codes.getText().toString();

                if (StringUtils.isEmpty(mobile)) {//电话号码为空
                    phone.setError(getString(R.string.error_phone_empty));
                } else if (!StringUtils.moreThanLength(mobile, 11)) {//手机号码位数不正确
                    phone.setError(getString(R.string.error_phone_length));
                } else if (StringUtils.isEmpty(codesStr)) {
                    codes.setError(getString(R.string.error_mobilecode_empty));
                } else if (StringUtils.isEmpty(passwordStr)) {
                    password.setError(getString(R.string.error_password_empty));
                } else if (passwordStr.length() < 6 || passwordStr.length() > 15) {
                    password.setError(getString(R.string.error_password_size));

                } else if (StringUtils.isEmpty(passwordConfirmStr)) {
                    passwordConfirm.setError(getString(R.string.error_password_empty));
                } else if (passwordConfirmStr.length() < 6 || passwordConfirmStr.length() > 15) {
                    passwordConfirm.setError(getString(R.string.error_password_size));
                } else if (!passwordConfirmStr.equals(passwordStr)) {
                    passwordConfirm.setError(getString(R.string.error_password_repeat));
                } else {
                    commitToSever(mobile, passwordStr, stationStr, codesStr);
                }
                break;
        }
    }

    private void commitToSever(String mobileStr, String passwordStr, String stationStr, String codeStr) {
        final ProgressDialogCompat progress = new ProgressDialogCompat(this);
        progress.show();
        Registration data = new Registration();
        data.setMobile(mobileStr);
        data.setPassword(MD5.md5(passwordStr).substring(0, 16).toUpperCase());
        data.setSmsCode(codeStr);
        data.setStationId(stationStr);
        progress.setMessage(getString(R.string.prompt_loading));
        Call<Result<User>> call = PalmRetrofit.getInstance().getApi().register(new Request<>(data));
        call.enqueue(new PalmCallBack<User>(this) {
            @Override
            public void succeed(User user) {
                if (!progress.isCanceled()) {
                    progress.dismiss();
                    PromptUtil.showToast(RegisterActivity.this, getString(R.string.prompt_register_success));
                    finish();
                }
            }

            @Override
            public void failed(String message) {
                if (!progress.isCanceled()) {
                    progress.dismiss();
                    Toast.makeText(getContext(), StringUtils.isEmpty(message) ? getString(R.string.error_register_fail) : message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void getCodes() {
        String mobile = phone.getText().toString();
        if (StringUtils.isEmpty(mobile) || !StringUtils.moreThanLength(mobile, 11)) {
            phone.setError(getString(R.string.error_phone_length));
        } else {
            PalmSMS sms = new PalmSMS();
            sms.setMobile(mobile);
            sms.setType(PalmSMS.register);
            PalmRetrofit.getInstance().getApi().sendSMS(new Request<>(sms)).enqueue(new PalmCallBack<Void>(this) {
                @Override
                public void succeed(Void aVoid) {
                    Toast.makeText(getContext(), "短信已发送", Toast.LENGTH_SHORT).show();
                    countDown = CountDownUtils.countDownButton(getCodes, 60, "重新发送");
                }

                @Override
                public void failed(String message) {
                    Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
                }
            });
        }

    }


}
